package com.banyuan.servlet.author;

import com.banyuan.dao.AuthorDao;
import com.banyuan.domain.Author;
import com.banyuan.utils.DBUtils;
import com.banyuan.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/author/updateInfoPage")
public class UpdateInfoPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        ResultSet resultSet = null;
        try {
             resultSet = AuthorDao.selectAuthorById(Integer.parseInt(id));
            Author author = DBUtils.resultToBeanOne(resultSet, Author.class);
            req.setAttribute("author",author);
            req.getRequestDispatcher("/WEB-INF/pages/author/updateInfo.jsp").forward(req,resp);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeBy(resultSet);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
